#include "home.h"
#include "creditutility.h"
#include <QApplication>
#include <QStyleFactory>
#include <QPalette>
#include <QLocale>
#include <QTranslator>

#include <QFile>
#include <QtNetwork/QNetworkInterface>
#include <QJsonDocument>
#include <QJsonParseError>
#include <QJsonObject>

QStringList getMacs() {
    QList<QNetworkInterface> nets = QNetworkInterface::allInterfaces();

    int nCnt = nets.count();
    QStringList strMacAddrs{};
    for(int i = 0; i < nCnt; i++)
    {
        if(nets[i].flags().testFlag(QNetworkInterface::IsUp) &&
            nets[i].flags().testFlag(QNetworkInterface::IsRunning) &&
            !nets[i].flags().testFlag(QNetworkInterface::IsLoopBack))
        {
            for(int j = 0; j < nets[i].addressEntries().size(); j++)
            {
                if(nets[i].addressEntries().at(j).ip() != QHostAddress::LocalHost &&
                    nets[i].addressEntries().at(j).ip().protocol() == QAbstractSocket::IPv4Protocol)
                {
                    strMacAddrs.append(nets[i].hardwareAddress());
                }
            }
        }
    }

    return strMacAddrs;
}

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QPalette palette;

    palette.setColor(QPalette::Window, QColor(240, 240, 240));
    palette.setColor(QPalette::WindowText, Qt::black);
    palette.setColor(QPalette::Base, Qt::white);
    palette.setColor(QPalette::AlternateBase, QColor(240, 240, 240));
    palette.setColor(QPalette::ToolTipBase, Qt::white);
    palette.setColor(QPalette::ToolTipText, Qt::black);
    palette.setColor(QPalette::Text, Qt::black);
    palette.setColor(QPalette::Button, QColor(240, 240, 240));
    palette.setColor(QPalette::ButtonText, Qt::black);
    palette.setColor(QPalette::BrightText, Qt::red);
    palette.setColor(QPalette::Highlight, QColor(0, 120, 215));
    palette.setColor(QPalette::HighlightedText, Qt::white);

    QApplication::setPalette(palette);
    QApplication::setStyle("Fusion");

    QTranslator translator;
    const QStringList uiLanguages = QLocale::system().uiLanguages();
    for (const QString &locale : uiLanguages) {
        const QString baseName = "CreditSystem_" + QLocale(locale).name();
        if (translator.load(":/i18n/" + baseName)) {
            a.installTranslator(&translator);
            break;
        }
    }

    MODE mode = QueryResult;

    QFile settings("settings");
    if(settings.exists() && settings.open(QFile::ReadOnly)) {
        QByteArray byteArray=decrypt(settings.readAll());
        settings.close();

        QJsonParseError parseError;
        QJsonDocument doc = QJsonDocument::fromJson(byteArray,&parseError);
        if (!doc.isEmpty() && parseError.error == QJsonParseError::NoError) {
            QJsonObject object = doc.object();
            if(getMacs().contains(object["mac"].toString())) {
                int result = object["mode"].toInt(4);
                if(result >= 0 && result < 8) mode = MODE(result);
            }
        }
    }

    Home w(mode);
    /*
    enum MODE {
        ResultOverview=1,
        EditActivity=2,
        QueryResult=4,
        FullAccess=7
    };
    */
    w.show();
    return a.exec();
}
